using QFramework;
using LitJson;
using UnityEngine;
using System.Text;
using System;

public class UpdateAgvInfoCommand : AbstractCommand
{
    private StringBuilder stringBuilder;

    public UpdateAgvInfoCommand()
    {
        stringBuilder = new();
    }

    protected override void OnExecute()
    {
        stringBuilder.Clear();
        stringBuilder.Append(this.GetSystem<INetworkSystem>().HttpBaseUrl);
        stringBuilder.Append(RequestUrl.updateAgvInfoWSUrl);
        MyLogger.Log("updateAgvInfoUrl: " + stringBuilder);
        if (this.GetSystem<INetworkSystem>().ServerType == ServerType.Local)
        {

#if UNITY_EDITOR
            this.GetSystem<INetworkSystem>().GetLocal(
                url: stringBuilder.ToString(),
                succStr: OnGetResponseSuccess,
                errorStr: OnGetResponseFailure
                );
#endif
        }
        else
        {
            this.GetSystem<INetworkSystem>().ConnectWs(RequestUrl.updateAgvInfoWSUrl,
                serverType: ServerType.NdcServer,
                GetRespondAction: OnGetResponseSuccess);
            /*this.GetSystem<INetworkSystem>().GETHTTP(
                url: stringBuilder.ToString(),
                succStr: OnGetResponseSuccess,
                errorStr: OnGetResponseFailure
                );*/
        }
    }

    private void OnGetResponseSuccess(string succStr)
    {
        MyLogger.Log($"Receive {succStr}");
        JsonData jd = JsonMapper.ToObject(succStr);

#if UNITY_STANDALONE_WIN
        var properties = typeof(NdcData).GetProperties();
        this.GetModel<IAgvModel>().NdcInfoDic.Clear();

        foreach (System.Reflection.PropertyInfo propInfo in properties)
        {
            var propName = propInfo.Name;
            propName = propName.Remove(0, 1);
            if (!jd.ContainsKey(propName))
                continue;
            NdcInfo info = new NdcInfo();
            {
                if (jd[propName].ContainsKey("carId"))
                    info.agvId = ((int)jd[propName]["carId"]).ToString();
                if (jd[propName].ContainsKey("posX"))
                    info.posX = (float)jd[propName]["posX"];
                if (jd[propName].ContainsKey("posY"))
                    info.posY = (float)jd[propName]["posY"];
                if (jd[propName].ContainsKey("forkHeight"))
                    info.forkHeight = (float)jd[propName]["forkHeight"];
                if (jd[propName].ContainsKey("loadStatus"))
                    info.isLoaded = (int)jd[propName]["loadStatus"] == 1;
                //isLoaded = ((int)jd[propName]["sensor"] != 0),
                if (jd[propName].ContainsKey("vehicleAngle"))
                    info.angle = (float)(jd[propName]["vehicleAngle"]);
            };
            if (jd[propName] != null)
            {
                info.pushStatus = (int)jd[propName]["forkStatus"];
            }
            this.GetModel<IAgvModel>().NdcInfoDic.Add(info.agvId, info);
            //this.GetModel<IAgvModel>().AddInfo(info);
        }
#else
        if(jd.IsObject)
        {
            NdcInfo info = new NdcInfo();
            string agvId = "";
            if (jd[agvId].ContainsKey("carId"))
                info.agvId = ((int)jd[agvId]["carId"]).ToString();
            if (jd[agvId].ContainsKey("posX"))
                info.posX = (float)jd[agvId]["posX"];
            if (jd[agvId].ContainsKey("posY"))
                info.posY = (float)jd[agvId]["posY"];
            if (jd[agvId].ContainsKey("forkHeight"))
                info.forkHeight = (float)jd[agvId]["forkHeight"];
            if (jd[agvId].ContainsKey("loadStatus"))
                info.isLoaded = (int)jd[agvId]["loadStatus"] == 1;
            if (jd[agvId].ContainsKey("vehicleAngle"))
                info.angle = (float)(jd[agvId]["vehicleAngle"]);
            if (jd[agvId].ContainsKey("forkStatus"))
                info.pushStatus = (int)jd[agvId]["forkStatus"];

            this.GetModel<IAgvModel>().NdcInfoDic.Add(info.agvId, info);
        }
#endif
        this.SendEvent(new UpdateAgvInfoEvent());
    }

    private void OnGetResponseFailure(string failStr)
    {
        Debug.LogError($"{failStr}");
    }

    internal sealed class NdcData
    {
        public NdcInfo _30 { get; set; }
        public NdcInfo _59 { get; set; }
    }
}
